//
//  ViewController.m
//  eGifr
//
//  Created by Phien Tram on 8/4/13.
//  Copyright (c) 2013 eGiftr. All rights reserved.
//

#import "ViewController.h"
#import "UserHelper.h"
#import "AppDelegate.h"
#import "HomeViewController.h"
#import "WishCloudViewController.h"
#import "Constants.h"

@interface ViewController () <HomeViewDelegate, WishCloudViewDelegate>
{
    BOOL isFirstTimeLaunch;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    _myWishClouds = [NSMutableArray array];
    isFirstTimeLaunch = YES;
    self.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MyWishListNavigation"];
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    if ([UserHelper isUserRegistered]) {
        // Start 
        
    } else {
        //Present the WishCloud
        if (_myWishClouds.count == 0) {
            WishCloudViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"WishCloud"];
            viewController.delegate = self;
            [self presentViewController:viewController animated:YES completion:^{
                
            }];
        } else {
            //present modal view
            [self showHomeView];
        }
    }
    
    if (isFirstTimeLaunch) {
        isFirstTimeLaunch = NO;
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showHomeView) name:kShouldShowLoginNotificationName object:nil];
    }
}

- (void)viewDidUnload
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    [super viewDidUnload];
}

#pragma mark - Private methods

- (void)showHomeView
{
    NSLog(@"+++showHomeView+++");
    
    UINavigationController *naviController = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeNavigationController"];
    HomeViewController *homeViewController = [[naviController viewControllers] objectAtIndex:0];
    homeViewController.delegate = self;
    
    //to change the new login process
//    UINavigationController *naviController = [self.storyboard instantiateViewControllerWithIdentifier:@"AppLaunchLandingPageNavigationController"];
    if ([SharedAppDelegate.window.rootViewController presentedViewController]) {
        [[SharedAppDelegate.window.rootViewController presentedViewController] dismissViewControllerAnimated:NO completion:^{
            [SharedAppDelegate.window.rootViewController presentViewController:naviController animated:YES completion:nil];
        }];
    } else {
        [SharedAppDelegate.window.rootViewController presentViewController:naviController animated:YES completion:nil];
    }
}

#pragma mark - HomeViewController delegate

- (void)homeViewLoginSuccessful:(HomeViewController *)viewController
{
    [viewController dismissViewControllerAnimated:YES completion:^{
        UIViewController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MyWishListNavigation"];
        [self.slidingViewController anchorTopViewOffScreenTo:ECRight animations:nil onComplete:^{
            CGRect frame = self.slidingViewController.topViewController.view.frame;
            self.slidingViewController.topViewController = newTopViewController;
            self.slidingViewController.topViewController.view.frame = frame;
            [self.slidingViewController resetTopView];
        }];
    }];
}

#pragma mark - WishCloudViewController delegate

- (void)wishCloudViewDone:(WishCloudViewController *)viewController
{
    NSMutableArray *mutableArray = [NSMutableArray array];
    for (UILabel *label in viewController.myWishCloudLabels) {
        [mutableArray addObject:label.text];
    }
    self.myWishClouds = mutableArray;
    [viewController dismissViewControllerAnimated:YES completion:^{
        
    }];
}

@end
